import type { StrictModifiers, Placement } from "@popperjs/core";

interface ModifierProps {
  offset?: number;
  arrow?: HTMLElement;
  arrowOffset?: number;
  gpuAcceleration?: boolean;
  fallbackPlacements?: Array<Placement>;
}

export default function buildModifier(
  props: ModifierProps,
  externalModifiers: StrictModifiers[] = []
) {
  const { arrow, arrowOffset, gpuAcceleration, fallbackPlacements, offset } =
    props;
  const modifiers: Array<StrictModifiers> = [
    {
      name: "offset",
      options: {
        offset: [0, offset ?? 12],
      },
    },
    {
      name: "preventOverflow",
      options: {
        padding: {
          top: 2,
          bottom: 2,
          left: 5,
          right: 5,
        },
      },
    },
    {
      name: "flip",
      options: {
        padding: 5,
        fallbackPlacements: fallbackPlacements ?? [],
      },
    },
    {
      name: "computeStyles",
      options: {
        gpuAcceleration,
        adaptive: gpuAcceleration,
      },
    },
  ];

  if (arrow) {
    modifiers.push({
      name: "arrow",
      options: {
        element: arrow,
        padding: arrowOffset ?? 5,
      },
    });
  }
  modifiers.push(...externalModifiers);
  return modifiers;
}
